﻿using System;
using System.Windows.Forms;
using FoundationClass;
using FoundationClass.Entity;
using Telerik.WinControls.UI;

namespace P2MARY_INTERNET.Controls
{
    public partial class EmployeeForm : UserControl
    {
        public EmployeeForm()
        {
            InitializeComponent();
            InitializeUserData();
            InitializeControl();
            lblValidate.Text = string.Empty;
        }

        private void InitializeControl()
        {
            pnlEmpData.Enabled = btnDeleteEmp.Enabled = lstEmployee.SelectedIndex > -1;
        }

        private void InitializeUserData()
        {
            try
            {
                var userList = SecurityHandler.GetAllUsers();
                if (userList == null)
                    return;

                foreach (var u in userList)
                {
                    var item = new RadListDataItem(u.FirstName +"  " + u.LastName, u);
                    lstEmployee.Items.Add(item);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(@"InitializePromotionDatasss : " + ex.Message);
            }
        }

        private void lstEmployee_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            try
            {
                if (lstEmployee.SelectedIndex == -1)
                {
                    ClearForm();
                    return;
                }
                pnlEmpData.Enabled = btnDeleteEmp.Enabled = lstEmployee.SelectedIndex > -1;

                var item = lstEmployee.Items[lstEmployee.SelectedIndex];
                var val = item.Value as User;
                if (val == null) return;

                txtEmpId.Text = val.Id;
                txtAddress.Text = val.Address;
                drpPosition.SelectedIndex = val.Position;
                drpPosition.Items[val.Position].Selected = true;
                dateStart.Text = val.DateStart.ToShortDateString();
                txtEmpName.Text = val.FirstName;
                radMale.IsChecked = val.Gender == "M";
                radFemale.IsChecked = val.Gender == "F";
                txtEmpLastName.Text = val.LastName;
                txtNickName.Text = val.NickName;
                txtTel.Text = val.Phone;
                txtWage.Value = val.Wage;
                drpWorkTime.Text = val.WorkTime;
            }
            catch (Exception ex)
            {
                MessageBox.Show(@"lstEmployee_SelectedIndexChanged : " + ex.Message);
            }
        }

        private void ClearForm()
        {
            txtEmpId.Text = string.Empty;
            txtAddress.Text = string.Empty;
            drpPosition.SelectedIndex = 1;
            dateStart.Text = string.Empty;
            txtEmpName.Text = string.Empty;
            radMale.IsChecked = true;
            txtEmpLastName.Text = string.Empty;
            txtNickName.Text = string.Empty;
            txtTel.Text = string.Empty;
            txtWage.Value = 200;
            drpWorkTime.Text = string.Empty;
            dateStart.Value = DateTime.Now;
        }

        private void btnAddEmp_Click(object sender, EventArgs e)
        {
            try
            {
                var dlg = new EmployeeDialog { StartPosition = FormStartPosition.CenterParent };
                User user;
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    user = dlg.Employee;
                    string tag = string.Format("{0}  {1}", user.FirstName, user.LastName);
                    var radL = new RadListDataItem(tag, user);
                    lstEmployee.Items.Add(radL);
                    SecurityHandler.AddNewUser(user);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(@"btnAddEmp :" + ex.Message, @"Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnDeleteEmp_Click(object sender, EventArgs e)
        {
            try
            {
                var user = lstEmployee.SelectedItem.Value as User;
                if (user == null)
                    return;
                var confirm = new ConfirmDialog {StartPosition = FormStartPosition.CenterParent};
                if (DialogResult.OK == confirm.ShowDialog())
                {
                    SecurityHandler.RemoveUser(user.Id);
                    lstEmployee.Items.Remove(lstEmployee.SelectedItem);
                    pnlEmpData.Enabled = btnDeleteEmp.Enabled = lstEmployee.Items.Count != 0;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(@"btnDeleteEmp :" + ex.Message, @"Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnEditEmp_Click(object sender, EventArgs e)
        {
            try
            {
                if(txtEmpName.Text.Length == 0 ||txtEmpLastName.Text.Length == 0 || txtNickName.Text.Length == 0 || txtAddress.Text.Length==0 || txtTel.Text.Length ==0)
                {
                    lblValidate.Text = @"กรูณากรอกข้อมูลให้ครบ";
                    return;
                }
                var user = lstEmployee.Items[lstEmployee.SelectedIndex].Value as User;
                user.Id = txtEmpId.Text;
                user.Address = txtAddress.Text;
                user.DateStart = Convert.ToDateTime(dateStart.Text);
                user.FirstName = txtEmpName.Text;
                user.Gender = radMale.IsChecked ? "M" : "F";
                user.LastName = txtEmpLastName.Text;
                user.NickName = txtNickName.Text;
                user.Phone = txtTel.Text;
                user.Position = drpPosition.SelectedIndex;
                user.Wage = txtWage.Value;
                user.WorkTime = drpWorkTime.Text;
                lstEmployee.SelectedItem.Value = user;
                lstEmployee.SelectedItem.Text = string.Format("{0}  {1}", txtEmpName.Text, txtEmpLastName.Text);
                SecurityHandler.UpdateUser(user);
                MessageBox.Show(@"บันทึกข้อมูลเรียบร้อย");
            }
            catch (Exception ex)
            {
                MessageBox.Show(@"btnEditEmp :" + ex.Message, @"Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
